package cn.wbu.zyh.service.impl;

import cn.wbu.zyh.dto.InvitationDTO;
import cn.wbu.zyh.dto.TeamMemberDTO;
import cn.wbu.zyh.entity.Invitation;
import cn.wbu.zyh.entity.TeamMember;
import cn.wbu.zyh.mapper.InvitationMapper;
import cn.wbu.zyh.mapper.TeamMapper;
import cn.wbu.zyh.service.InvitationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class InvitationServiceImpl implements InvitationService {
    @Autowired
    private InvitationMapper invitationMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Override
    public void createInvite(InvitationDTO invitationDTO) {
        Invitation invitation = new Invitation();
        invitation.setTeamId(invitationDTO.getTeamId());
        invitation.setTeamName(invitationDTO.getTeamName());
        invitation.setInviteeId(invitationDTO.getInviteeId());
        invitation.setInviterId(invitationDTO.getInviterId());
        invitation.setStatus(0);  // 初始状态为0: 待接受
        invitationMapper.insert(invitation);

        // 通过WebSocket发送通知
        InvitationDTO notificationDTO = new InvitationDTO();
        notificationDTO.setTeamId(invitationDTO.getTeamId());
        notificationDTO.setTeamName(invitationDTO.getTeamName());
        notificationDTO.setInviterId(invitationDTO.getInviterId());
        notificationDTO.setInviteeId(invitationDTO.getInviteeId());
        notificationDTO.setMessage(invitationDTO.getInviterId() + " 邀请您加入团队 " + invitationDTO.getTeamName());

        // 发送到特定用户的主题
        messagingTemplate.convertAndSend("/topic/invites/" + invitationDTO.getInviteeId(), notificationDTO);
        // 同时发送到通用主题（用于演示）
        messagingTemplate.convertAndSend("/topic/invites", notificationDTO);
    }

    @Override
    public List<Invitation> getPendingInvites(String username) {
        List<Invitation> invitations = invitationMapper.findPendingInvites(username);

        return invitations;
    }

    @Override
    public List<Invitation> getInviteHistory(String username) {
        List<Invitation> invitations = invitationMapper.findInviteHistory(username);

        return invitations;
    }
    @Override
    public void acceptInvite(TeamMemberDTO teamMemberDTO) {
        Invitation invitation = invitationMapper.findInvitationByUsername(teamMemberDTO.getUsername());
        TeamMember teamMember = new TeamMember();
        if (invitation != null && invitation.getStatus() == 0) {
            invitation.setStatus(1); // 已接受
            teamMember.setTeamId(teamMemberDTO.getTeamId());
            teamMember.setUsername(teamMemberDTO.getUsername());
            teamMember.setRole(2);
            invitationMapper.updateInvitation(invitation);
            teamMapper.updateHasTeam(teamMemberDTO.getUsername(),1);
            teamMapper.joinTeam(teamMember);
        }
    }

    public void rejectInvite(String inviteId) {
        Invitation invitation = invitationMapper.findInvitationByUsername(inviteId);
        if (invitation != null && invitation.getStatus() == 0) {
            invitation.setStatus(2); // 已拒绝
            invitationMapper.updateInvitation(invitation);
        }
    }

}
